<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Chmanager extends CH_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	 /**
         * Constructor
         */
	function __construct() { 
		parent::__construct(); 
		$this->init();
		
		/*$this->load->model('contents_model');
		$this->load->model('popup_model');
		$this->load->model('common_model');*/
		$this->load->helper(array('directory'));
		$this->load->model(array('contents_model','board_model','common_model','member_model'));
    } 

	/*function parse($data){
		if(is_array($data)){
			$data = array_merge($data,array('_theme'=>$this->config->item('ch_theme')));
		}else{
			$data = array('_theme'=>$this->config->item('ch_theme'));
		}
		$this->load->view('chmanager',$data);	
	}*/
	

	/**
	 * _remap
	 *
	 * URI 재정의
	 * @access      public
	 * @return      string
	 */
	/*public function _remap($method, $params = array()){
		//@method assign
		//if(is_array($this->$method())) $data += $this->$method();
		$data = $this->$method();
		//@result assign
		
		if(strpos($this->uri->uri_string(),'ajax_') > 0){
			//$this->_parse($this->uri->uri_string()); //마스터 페이지 제외
			$this->load->view($this->uri->uri_string(),$data);
		}else if(strpos($this->uri->uri_string(),'index') > 0){ //인덱스 제외
			$this->load->view($this->uri->uri_string(),$data);
		}else{
			$this->parse($data);
		}
	}*/
	/*
	@관리자 메인
	*/
	public function index(){
		$url =$this->uri_church();	
		$limit_date = date("Y-m-d", strtotime(date("Y-m-d")." -10 day"));
		$new_member = $this->member_model->list_member_index(array('reg_date >='=>$limit_date));
		$data = array(
				'_title' => '관리자 메인',
				'_content' => $url,
				'_theme'=> $this->config->item('ch_theme'),
				'new_member'=> $new_member
		);
		return $data ;
	}
	
	function ajax_weblog(){
		$url =$this->uri_church();
		//통계
		$this->load->library('bbclone');
		$access = $this->bbclone->getAsscess();
		$stat = $this->input->get('stat');
		$data = array(
				'_title' => '관리자메인통계',
				'_content' => $this->config->item('ch_manager_root').'/stat/index_'.$stat.'_log',
				'_use_theme' => 'N',
				'access' => $access 
		);
		
		return $data;
	}
	
		
	/*
	 @게시판 관리
	*/
	public function board(){
		
		$uri_array = segment_explode($this->uri->uri_string());
		$array_segment = array_keys($this->uri->segment_array(), "page");
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$id = in_array('id', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'id'))) : null;
		$no = in_array('no', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'no'))) : null;
		$action = in_array('board', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'board'))) : null;

		$uri_segment = in_array('page', $uri_array) ? $array_segment[0]+1 : 0;
		$url = $this->common_model->get_index_uri(3);
		$board_info = $this->board_model->board_info($id);
		$this->load->library('CHboard',array('config'=>$board_info,'table_name'=>'ch_board_'.$id,'skin'=>'basic'));
		$view = $this->chboard->getOutter();
		$board_name = 'ch_board_'.$id;
			
		switch($action){
			case('list'):
				$param['sch_keyname'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_keyname')));
				$param['sch_keyword'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_keyword')));
				$sch_url_pattern = search_param_url($param);
				
				$param = array_merge($param,array('page'=>$page,'id'=>$id,'board_name'=>$board_name));
				
				$listnum = $board_info['list_cnt'];
				$count = $this->board_model->list_total($param);
				$this->chboard->dress('board_list',$this->chboard->boardlist($param),$view);				
				//@paging
				$this->load->library('pagination');
				$config['base_url'] = '/chmanager/board/list/id/'.$id.'/'.$sch_url_pattern.'/page';
				$config['total_rows'] = $count;
				$config['per_page'] = $listnum;
				$config['use_page_numbers'] = true;
				$config['uri_segment'] = $uri_segment;
				$this->pagination->initialize($config);
				
				$this->chboard->dress('PAGING',$this->pagination->create_links(),$view);
				$this->chboard->dress('skin_name',$board_info['board_skin'],$view);
				$this->chboard->dress('id',$id,$view);
				$this->chboard->dress('brd_btn_write','/chmanager/board/write/id/'.$id,$view);
				$view = $this->chboard->removeTagSelected('no_data','',$view);
			break;
			case('write'):
				$view = $this->chboard->boardWrite(array('page'=>$page,'id'=>$id,'no'=>$no,'board_name'=>$board_name));
			break;
			case('reply'):
				$view = $this->chboard->boardReply(array('page'=>$page,'id'=>$id,'no'=>$no,'board_name'=>$board_name));
			break;
			case('view'):
				$view = $this->chboard->boardView(array('page'=>$page,'id'=>$id,'no'=>$no,'board_name'=>$board_name));
			break;
		}
		$view = $this->chboard->removeAllTags($view);
		
		$data = array(
				'_title' => '게시판 관리',
				'_group' => 'homepage',
				'_content' => $url,
				'id'=>$id,
				'view'=>$view
		);
		
		return $data;
	}
	
	function boardDbjob(){
		$id = $this->input->post('id',true);
		$title = $this->input->post('title',true);
		$writer = $this->input->post('writer',true);
		$content = $this->input->post('content',true);
		$dbjob = ($this->input->post('dbjob')) ? $this->input->post('dbjob',true) : 'i';
		
		switch($dbjob){
			case('i'):
				$msg = '저장되었습니다.';
				$data = array(
						'title' => $title ,
						'writer' => $writer ,
						'content' => $content ,
						'auth_uid' => 'admin',
						'depth' => 0,
						'ansnum'=> 0,
						'view_count'=> 0,
						'register_date'=>  date("Y-m-d H:i",time())
				);
			break;
			case('r'):
				$msg = '저장되었습니다.';
				$data = array(
						'title' => $title ,
						'writer' => $writer ,
						'content' => $content ,
						'auth_uid' => 'admin',
						'view_count'=> 0,
						'register_date'=>  date("Y-m-d H:i",time())
				);
			break;
			case('u'):
				$msg = '수정되었습니다.';
				$data = array(
						'title' => $title ,
						'writer' => $writer ,
						'content' => $content
						
				);
			break;
			case('d'):
				$msg = '삭제 되었습니다.';
				$data = null;
			break;
		}
		
		if(isset($_FILES['files'])){
			/*file upload begin*/
			$upload_path = convert_path('/upload/board/'.$id);
			$config['upload_path'] = $upload_path;
			$config['allowed_types'] = 'jpg|gif|png|zip|rar|doc|hwp|pdf|ppt|xls|pptx|docx|xlsx';
			$config['max_size']	= '10240'; //10MB
			//$config['max_width']  = '1024';
			//$config['max_height']  = '768';
			$config['encrypt_name'] = true;
			$config['remove_spaces'] = true;
			$this->load->library('upload', $config);
			$this->upload->mkdir($upload_path);
		}
		
		if($this->board_model->board_dbjob($data)){
			if(isset($_FILES['files'])){
				$file = $this->upload->do_multi_upload('files');
				if($this->upload->display_errors()){
					$msg = $this->upload->display_errors('','');
				}else{
					$this->board_model->board_file($file);
				}
			}
			$this->alert->jalert($msg, base_url('/chmanager/board/list/id/'.$id));
		}else{
			$this->alert->jalert('에러', '','back');
		}		
		exit;
	}
	
	
	function boardCommentDbjob(){
		$board_id = $this->input->post('board_id',true);
		$board_no = $this->input->post('board_no',true);
		$b_comment = $this->input->post('b_comment',true);
		$user_id = $this->input->post('user_id',true);
		$name = $this->input->post('name',true);
		$content = $this->input->post('content',true);
		$dbjob = ($this->input->post('dbjob')) ? $this->input->post('dbjob',true) : 'i';
	
		switch($dbjob){
			case('i'):
				$msg = '저장되었습니다.';
				$data = array(
						'board_no' => $board_no ,
						'user_id' => $user_id ,
						'name' => $name ,
						'b_comment' => $b_comment,
						'register_ip'=> $this->input->ip_address(),
						'register_date'=>  date("Y-m-d H:i",time())
				);
			break;
			case('u'):
				$msg = '수정되었습니다.';
				$data = array(
						'b_comment' => $b_comment
				);
			break;
			case('d'):
				$data = null;
				$msg = '삭제 되었습니다.';
			break;
		}
		
		$this->board_model->board_comment_dbjob($data);
		$this->alert->jalert($msg, $this->input->server('HTTP_REFERER'));
	}
	
	/*
	@컨텐츠 관리	
	*/
	public function contents(){
		$this->load->library('databasei');
		$this->load->library('tree');
	
		$url =$this->uri_church();
		if($this->input->post('dbjob')){
			$msg='';
			switch($this->input->post('dbjob')){
				case('i'): //컨텐츠(파일) 등록
					$msg='등록 되었습니다.';
					$this->contents_model->create();
				break;
				case('u'): //컨텐츠(파일) 수정
					$msg='수정 되었습니다.';
					$this->contents_model->update();
				break;
				case('d'): //컨텐츠(파일) 삭제
					$msg='삭제 되었습니다.';
					$this->contents_model->delete();
				break;
			}
			
			$rtn_link='';
			switch($this->input->post('page_type')){
				case('HTML'):
					$rtn_link = 'chmanager/contents/contents_html';
				break;
				case('BOARD'):
					$rtn_link = 'chmanager/contents/contents_board';
				break;
				case('GALLERY'):
					$rtn_link = 'chmanager/contents/contents_gallery';
				break;
				case('PLUGIN'):
					$rtn_link = 'chmanager/contents/contents_plugin';
					break;
				default:
					$rtn_link = 'chmanager/contents/contents_html';
				break;
			}
			$this->alert->jalert($msg, base_url($rtn_link."/id/".$this->input->post('id')));
			//redirect(base_url($rtn_link."/id/".$this->input->post('id')));
			exit;
		}
			
		$id = ($this->uri->segment(5)) ? $this->uri->segment(5) : $this->input->post('id');
		
		$data = array(
				'_title' => '컨텐츠관리',
				'_group' => 'homepage',
				'_content' => $url,
				'id'=> $id
		);
		
		$data = array_merge($data, $this->contents_model->select($id));
		return $data;
	}
	/*@
		uri 설정
	*/
	function uri_church(){
		$url = $this->uri->uri_string();
		if($this->uri->total_segments() >= 3){
			$url = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3);
		}
		$url = str_replace('html/','',$url);
		return $url;
	}
	
	/**
	 * 설교방송 목록
	 *
	 * @access	public
	 * @return	result
	 */
	function sermon(){
		$url =$this->uri_church();
		$data = array(
				'_title' => '설교방송 관리',
				'_content' => $url
		);
		return $data ;
	}
	
	/**
	 * 설교방송 폼
	 *
	 * @access	public
	 * @return	result
	 */
	function sermonForm(){
		$url =$this->uri_church();
		$data = array(
				'_title' => '설교방송 관리',
				'_content' => $url
		);
		return $data ;
	}
	
	/**
	 * 우편번호 찾기
	 *
	 * @access	public
	 * @return	result
	 */
	function ajax_find_post(){
		$url = $this->uri_church();
		$data = array(
				'_title' => '우편번호 검색',
				'_content' => $url,
				'obj1' => $this->input->get('obj1'),
				'obj2' => $this->input->get('obj2'),
				'obj3' => $this->input->get('obj3')
		);
	
		$data = array_merge($data,array('rst'=>$this->common_model->select_post()));
		return $data ;
	}
	
	
	
	/*
	@컨텐츠 메뉴
	*/
	
	
	/*
	@게시판 메뉴
	*/
}

/* End of file html.php */
/* Location: ./application/controllers/html.php */